package com.zmht.beike.web.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.TCaiminfo;
import com.zmht.beike.po.TCmonthinfo;
import com.zmht.beike.po.TCweekinfo;
import com.zmht.beike.po.TIaiminfo;
import com.zmht.beike.po.TIareacontent;
import com.zmht.beike.po.TIweekinfo;
import com.zmht.beike.vo.TargetConect;
import com.zmht.specedu.vo.Teacher;
/**
 * 目标管理Action
 * @author fengbo
 *
 */
public class TargetManageAction extends BaseAction {
	
	
	/**
	 * 获得所有目标 带分页
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getAllTarget(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		TargetConect targetContent = (TargetConect)form;
		List teachers = this.getStudentBiz().getStudentDAO().findByHQLObj("from Teacher");
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		if(null==targetContent.getTeacherId()){
			targetContent.setTeacherId(teacher.getTeacherId());
		}
		request.setAttribute("teachers", teachers);
		Integer index = 1;
		Integer pageSize = 5;
		if(null!=request.getParameter("currPage")){
			index = new Integer(request.getParameter("currPage"));
		}
		if(null!=request.getParameter("pageSize")){
			pageSize = new Integer(request.getParameter("pageSize"));
		}
	
		List list = this.getTargetManagerBiz().findAllTarget(targetContent,(index-1)*pageSize,pageSize);
		int total = this.getTargetManagerBiz().findAllTargetTotal(targetContent);
		
		request.setAttribute("currPage", index); 
		request.setAttribute("pageAmount", (int)Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		request.setAttribute("targets", list);
		
		request.setAttribute("targetContent",targetContent);
		return mapping.findForward("alltarget");
	}
	/**
	 * 编辑目标（根据参数选择是编辑集体目标 或者是自定义目标）
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward editTarget(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		TargetConect targetContent = (TargetConect)form;
		String targetId = request.getParameter("tid");
		String month = targetContent.getMonth()+"";
		String week = targetContent.getWeek()+"";
		String areaId = request.getParameter("areaId");
		Integer newTargetId = 0; //新目标id
		//复制目标
		if(new Integer(1).equals(targetContent.getTargetType())){
			//复制集体目标 以及目标下的内容
			newTargetId = this.getTargetManagerBiz().saveNewCTarget(new Integer(targetId),teacher.getTeacherId());
		}else{
			// 复制个别目标 以及下面的内容
			newTargetId = this.getTargetManagerBiz().saveNewITarget(new Integer(targetId),teacher.getTeacherId());
		}
		
		
		//如果是编辑集体目标（基本信息）
		if(new Integer(1).equals(targetContent.getTargetType())){
				//return new ActionForward("/beike/caiminfo.do?method=initCaiminfoEdit&targetId="+tid);
			if("0".equals(month)&&"0".equals(week)){//跳转到编辑目标页面
				//return new ActionForward("/jsp/mubiaozhiding/editMubiao.jsp?tid="+caiminfo.getId());
				return new ActionForward("/beike/caiminfo.do?method=initCaiminfoEdit&targetId="+targetId);
			}else if(!"0".equals(month)&&"0".equals(week)){
				//return new ActionForward("/jsp/mubiaozhiding/edit_Yuezhuti.jsp");
				return new ActionForward("/beike/cmonthinfo.do?method=initMonthInfoEdit&targetId="+targetId+"&month="+month+"&areaId="+areaId);
			}else{
				return new ActionForward("/beike/cweekinfo.do?method=initCweekinfoEdit&targetId="+targetId+"&month="+month+"&week="+week+"&areaId="+areaId);
				//return new ActionForward("/jsp/mubiaozhiding/edit_Zhouzhuti.jsp");
			}
		}else{
			if("0".equals(month)&&"0".equals(week)){
				//return new ActionForward("/jsp/mubiaozhiding/edit_newjiaoxue.jsp");
				return new ActionForward("/beike/iaiminfo.do?method=initIAiminfoEdit&targetId="+targetId);
				
			}else if(!"0".equals(month)&&"0".equals(week)){
				//return new ActionForward("/jsp/mubiaozhiding/edit_gebiezhouzhuti.jsp");
				return new ActionForward("/beike/iweekinfo.do?method=initIWeekinfoEdit&targetId="+targetId+"&month="+month+"&areaId="+areaId);
			}else{ //当月和周 均选择时
				
				//return new ActionForward("/jsp/mubiaozhiding/edit_gebiezhouzhuti.jsp");
				return new ActionForward("/beike/iareainfo.do?method=initAreaInfoEdit&targetId="+targetId+"&month="+month+"&week="+week+"&areaId="+areaId);
			}
		}
		
		
	}
	/**
	 * 获得我的（制定的）教学目标 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getMyTargetSearch(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Teacher teacher = (Teacher) request.getSession().getAttribute("current_user_session_key");
		
		TargetConect targetContent = (TargetConect)form;
		if(null==targetContent.getTeacherId()){
			targetContent.setTeacherId(teacher.getTeacherId());
		}
		
		
		
		Integer index = 1;
		Integer pageSize = 5;
		if(null!=request.getParameter("currPage")){
			index = new Integer(request.getParameter("currPage"));
		}
		if(null!=request.getParameter("pageSize")){
			pageSize = new Integer(request.getParameter("pageSize"));
		}
		List list = this.getTargetManagerBiz().getMyTarget(targetContent,(index-1)*pageSize,pageSize);
		int total = this.getTargetManagerBiz().getMyTargetTotal(targetContent);
		
		request.setAttribute("currPage", index); 
		request.setAttribute("pageAmount", (int)Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		request.setAttribute("targets", list);
		request.setAttribute("teacher", teacher);
		request.setAttribute("targetContent",targetContent);
		//return mapping.findForward("mytarget_content");
		return new ActionForward("/jsp/targetmanager/mytarget_contact.jsp");
	}
	
	/**
	 * 获得我收藏的教学目标
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getMyCollectionTarget(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try{
		List teachers =this.getStudentBiz().getStudentDAO().findByHQLObj("from Teacher");
		Teacher teacher = (Teacher) request.getSession().getAttribute("current_user_session_key");
		
		TargetConect targetContent = (TargetConect)form;
		if(null==targetContent.getTeacherId()){
			targetContent.setTeacherId(teacher.getTeacherId());
		}
		
		
		
		Integer index = 1;
		Integer pageSize = 5;
		if(null!=request.getParameter("currPage")){
			index = new Integer(request.getParameter("currPage"));
		}
		if(null!=request.getParameter("pageSize")){
			pageSize = new Integer(request.getParameter("pageSize"));
		}
		List list = this.getTargetManagerBiz().getMyFavorTarget(targetContent,(index-1)*pageSize,pageSize);
		int total = this.getTargetManagerBiz().getMyFavorTargetTotal(targetContent);
		
		request.setAttribute("currPage", index); 
		request.setAttribute("pageAmount", (int)Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		request.setAttribute("targets", list);
		request.setAttribute("teachers", teachers);
		request.setAttribute("targetContent",targetContent);
		}catch(Exception e){
			e.printStackTrace();
		}
		return mapping.findForward("mycoltarget_contact");
	}
	/**
	 * 编辑我制定的目标
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward editMyTarget(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//接收参数 targetType=${targetContent.targetType}&targetOption=${targetContent.targetOption}&month=${targetContent.month
		String targetType = request.getParameter("targetType");
		String targetId = request.getParameter("targetId");
		String month = request.getParameter("month");
		String week = request.getParameter("week");
		String areaId = request.getParameter("areaId");
		try{
			System.out.println("targetType=====areaId======"+areaId);
		if("1".equals(targetType)){//集体目标
			System.out.println("tatargetType month="+month+"===== week = "+week);
			if("0".equals(month)&&"0".equals(week)){//跳转到编辑目标页面
				//return new ActionForward("/jsp/mubiaozhiding/editMubiao.jsp?tid="+caiminfo.getId());
				return new ActionForward("/beike/caiminfo.do?method=initCaiminfoEdit&targetId="+targetId+"&areaId="+areaId);
			}else if(!"0".equals(month)&&"0".equals(week)){
				//return new ActionForward("/jsp/mubiaozhiding/edit_Yuezhuti.jsp");
				return new ActionForward("/beike/cmonthinfo.do?method=initMonthInfoEdit&targetId="+targetId+"&month="+month+"&areaId="+areaId);
			}else{
				return new ActionForward("/beike/cweekinfo.do?method=initCweekinfoEdit&targetId="+targetId+"&month="+month+"&week="+week+"&areaId="+areaId);
				//return new ActionForward("/jsp/mubiaozhiding/edit_Zhouzhuti.jsp");
			}
		}else{
			//个别目标
			if("0".equals(month)&&"0".equals(week)){
				//return new ActionForward("/jsp/mubiaozhiding/edit_newjiaoxue.jsp");
				return new ActionForward("/beike/iaiminfo.do?method=initIAiminfoEdit&targetId="+targetId);
				
			}else if(!"0".equals(month)&&"0".equals(week)){
				//return new ActionForward("/jsp/mubiaozhiding/edit_gebiezhouzhuti.jsp");
				return new ActionForward("/beike/iweekinfo.do?method=initIWeekinfoEdit&targetId="+targetId+"&month="+month+"&areaId="+areaId);
			}else{ //当月和周 均选择时
				
				//return new ActionForward("/jsp/mubiaozhiding/edit_gebiezhouzhuti.jsp");
				return new ActionForward("/beike/iareainfo.do?method=initAreaInfoEdit&targetId="+targetId+"&month="+month+"&week="+week+"&areaId="+areaId);
			}
			
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 删除我的目标
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delMyTarget(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String targetId = request.getParameter("targetId");
		String targetType = request.getParameter("targetType");
		if("1".equals(targetType)){//集体目标
			this.getTargetManagerBiz().deleteByHQL("delete from TCaiminfo t where t.id="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TCaimtostudent t where t.caimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TCareacontent t where t.caimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TCmonthcontent t where t.caimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TCmonthinfo t where t.caimId = "+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TCweekcontent t where t.caimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TCweekinfo t where t.caimId="+targetId);

			this.getTargetManagerBiz().deleteByHQL("delete from TAimtocollection t where t.caimId="+targetId);
		}else{
			this.getTargetManagerBiz().deleteByHQL("delete from TIaiminfo t where t.id="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TIaimtostudent t where t.iaimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TIareacontent t where t.iaimId = "+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TIareainfo t where t.iaimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TImonthcontent t where t.iaimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TImonthinfo t where t.iaimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TIweekcontent t where t.iaimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TIweekinfo t where t.iaimId="+targetId);
		
			this.getTargetManagerBiz().deleteByHQL("delete from TAimtocollection t where t.iaimId="+targetId);
		}
		
		return new ActionForward("/beike/targetmanager.do?method=getMyTargetSearch");
	}
	/**
	 * 删除我收藏的目标
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delMyCollectionTarget(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String targetId = request.getParameter("targetId");
		String targetType = request.getParameter("targetType");
		if("1".equals(targetType)){//集体目标
			this.getTargetManagerBiz().deleteByHQL("delete from TCaiminfo t where t.id="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TCaimtostudent t where t.caimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TCareacontent t where t.caimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TCmonthcontent t where t.caimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TCmonthinfo t where t.caimId = "+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TCweekcontent t where t.caimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TCweekinfo t where t.caimId="+targetId);
			
			this.getTargetManagerBiz().deleteByHQL("delete from TAimtocollection t where t.caimId="+targetId);
		}else{
			this.getTargetManagerBiz().deleteByHQL("delete from TIaiminfo t where t.id="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TIaimtostudent t where t.iaimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TIareacontent t where t.iaimId = "+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TIareainfo t where t.iaimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TImonthcontent t where t.iaimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TImonthinfo t where t.iaimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TIweekcontent t where t.iaimId="+targetId);
			this.getTargetManagerBiz().deleteByHQL("delete from TIweekinfo t where t.iaimId="+targetId);
		
			this.getTargetManagerBiz().deleteByHQL("delete from TAimtocollection t where t.iaimId="+targetId);
		}
		return new ActionForward("/beike/targetmanager.do?method=getMyCollectionTarget");
	}
	/**
	 * 根据实施日期获得集体教学目标下内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward ajaxCaiminfoTarget(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String data = request.getParameter("date");
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		
		String monthName = "";
		String weekName = "";
		List<TCaiminfo> caiminfo = this.getTargetManagerBiz().findByHQL("from TCaiminfo t where t.teacherId="+teacher.getTeacherId()+" and t.enactmentDate='"+data+"'");
		Integer caimId = 0;
		if(null!=caiminfo&&caiminfo.size()>0){caimId = caiminfo.get(0).getId();}
		
		List<TCmonthinfo> months = this.getTargetManagerBiz().findByHQL("from TCmonthinfo t where t.caimId = "+caimId);
		List<TCweekinfo> weeks = this.getTargetManagerBiz().findByHQL("from TCweekinfo t where t.caimId="+caimId);
		
		if(null!=months&&months.size()>0){monthName = months.get(0).getMonthThemeName();}
		if(null!=weeks&&weeks.size()>0){weekName = weeks.get(0).getWeekThemeName();}
		
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
			out.println("<root>");
				out.println("<monthName>" + monthName + "</monthName>");
				out.println("<weekName>" + weekName + "</weekName>");
			out.println("</root>");
		out.flush();
		out.close();
		return null;
	}
	/**
	 * 个别教学目标 根据实施日期 ajax 获得
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward ajaxIAiminfoTarget(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		String data = request.getParameter("date");
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		//获得个别月主题
		List<TIaiminfo> iaiminfo = this.getTargetManagerBiz().findByHQL("from TIaiminfo t where t.teacherId='"+teacher.getTeacherId()+"' and t.enactmentDate='"+data+"'");
		List<TIweekinfo> iweekinfo = this.getTargetManagerBiz().findByHQL("from TIweekinfo t where t.teacherId='"+teacher.getTeacherId()+"' and t.ctime='"+data+"'");
		Integer aimId = 0;
		
		
		
		
		String monthName = "";
		String weekName = "";
		if(null!=iaiminfo&&iaiminfo.size()>0){monthName = iaiminfo.get(0).getIaiminfoName();aimId = iaiminfo.get(0).getId();}
		if(null!=iweekinfo&&iweekinfo.size()>0){weekName = iweekinfo.get(0).getWeekThemeName();}
		
		List<TIareacontent> contents = this.getTargetManagerBiz().findByHQL("from TIareacontent t where t.iaimId = "+aimId);
		String hear = "";
		String pronunciation = "";
		String language = "";
		String communication = "";
		String lnowledge = "";
		String languageadd = "";
		String other = "";
		
		//适用年龄
		Integer age = 1;
		
		for(int i=0;i<contents.size();i++){
			TIareacontent c = contents.get(i);
			if("6".equals(c.getIareaType())){
				hear += c.getIareaContent()+"\n";
			}else if("7".equals(c.getIareaType())){
				pronunciation += c.getIareaContent()+"\n";
			}else if("8".equals(c.getIareaType())){
				language += c.getIareaContent()+"\n";
			}else if("9".equals(c.getIareaType())){
				communication += c.getIareaContent()+"\n";
			}else if("10".equals(c.getIareaType())){
				lnowledge += c.getIareaContent()+"\n";
			}else if("11".equals(c.getIareaType())){
				languageadd += c.getIareaContent()+"\n";
			}else if("12".equals(c.getIareaType())){
				other += c.getIareaContent()+"\n";
			}else{
				
			}
			age = c.getAge();
		}
		
		
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
			out.println("<root>");
			out.println("<monthName>" + monthName + "</monthName>");
			out.println("<weekName>" + weekName + "</weekName>");
			out.println("<hear>" +hear + "</hear>");
			out.println("<pronunciation>"+pronunciation+"</pronunciation>");
			out.println("<language>"+language+"</language>");
			out.println("<communication>"+communication+"</communication>");
			out.println("<lnowledge>"+lnowledge+"</lnowledge>");
			out.println("<languageadd>"+languageadd+"</languageadd>");
			out.println("<other>"+other+"</other>");
			
			out.println("<age>"+age+"</age>");
			out.println("</root>");
		out.flush();
		out.close();
		return null;
	}
}
